# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=jasper
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=4.2.5
pkgrel=2
pkgdesc="Reference implementation of JPEG-2000 Part-1 codec (ISO/IEC 15444-5) (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.ece.uvic.ca/~mdadams/jasper"
msys2_repository_url='https://github.com/jasper-software/jasper'
msys2_references=(
  "cpe: cpe:2.3:a:jasper_project:jasper"
)
license=("spdx:JasPer-2.0")
depends=("${MINGW_PACKAGE_PREFIX}-freeglut"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=(https://github.com/jasper-software/jasper/archive/version-${pkgver}/${_realname}-${pkgver}.tar.gz
        001-mingw-cmake.patch
        004-jasper-exports.patch
        005-fix-filename-buffer-overflow-msvcrt.patch)
sha256sums=('3f4b1df7cab7a3cc67b9f6e28c730372f030b54b0faa8548a9ee04ae83fffd44'
            '89f7653884c93e1cb32271c59213551d1231674bd12df00331c302971d5ac489'
            '0f9b9270edb3f19c08425d8e97588b4ebe6b494579e2b66bb14929d01c8b4059'
            '96db137a8eb657d1ee84339f3a8783637dbae8ebed5d32276ec8f76786fc1ab2')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "$srcdir"/${_realname}-version-${pkgver}

  apply_patch_with_msg \
    001-mingw-cmake.patch \
    004-jasper-exports.patch \
    005-fix-filename-buffer-overflow-msvcrt.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  msg2 "Building static library"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_INSTALL_LIBDIR=lib \
      ${_extra_config[@]} \
      -DJAS_ENABLE_OPENGL=OFF \
      -DJAS_ENABLE_LIBJPEG=ON \
      -DCMAKE_SKIP_RPATH=ON \
      -DJAS_ENABLE_SHARED=OFF \
      -S "${_realname}-version-${pkgver}" \
      -B "build-${MSYSTEM}-static"

  cmake --build "build-${MSYSTEM}-static"

  msg2 "Building shared library"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_INSTALL_LIBDIR=lib \
      ${_extra_config[@]} \
      -DJAS_ENABLE_OPENGL=OFF \
      -DJAS_ENABLE_LIBJPEG=ON \
      -DCMAKE_SKIP_RPATH=ON \
      -DJAS_ENABLE_SHARED=ON \
      -S "${_realname}-version-${pkgver}" \
      -B "build-${MSYSTEM}-shared"

  cmake --build "build-${MSYSTEM}-shared"
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}-static
  ctest . || msg2 "tests failed"
}

package_jasper() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}-shared"

  install -Dm 644 "${srcdir}"/${_realname}-version-${pkgver}/LICENSE.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE

  # split docs
  mkdir -p dest${MINGW_PREFIX}/share
  mv "${pkgdir}${MINGW_PREFIX}"/share/doc dest${MINGW_PREFIX}/share/doc
}

package_jasper-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mv dest/* "${pkgdir}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
